package com.taysear.parcel.fieldfactory;

import com.taysear.parcel.ParcelApplication;
import com.taysear.parcel.domain.EmployeeStatus;
import com.taysear.parcel.domain.EmployeeType;
import com.taysear.parcel.domain.Gender;
import com.taysear.parcel.domain.RouteStatus;
import com.taysear.parcel.domain.Station;
import com.taysear.parcel.domain.StationStatus;
import com.taysear.parcel.domain.Substation;
import com.taysear.parcel.util.Constants;
import com.vaadin.addon.jpacontainer.JPAContainer;
import com.vaadin.addon.jpacontainer.JPAContainerFactory;
import com.vaadin.addon.jpacontainer.fieldfactory.FieldFactory;
import com.vaadin.data.Item;
import com.vaadin.data.validator.StringLengthValidator;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.DateField;
import com.vaadin.ui.Field;
import com.vaadin.ui.NativeSelect;
import com.vaadin.ui.TextField;

@SuppressWarnings("serial")
public class RouteFieldFactory extends FieldFactory {
	
	ParcelApplication app;
	public final TextField routeId = new TextField("Route id");
	final DateField dateCreated = new DateField("Date created");
	NativeSelect routeStart = new NativeSelect("Route start");
	NativeSelect routeEnd = new NativeSelect("Route end");
	NativeSelect routeStatus = new NativeSelect("Status");

	public RouteFieldFactory(ParcelApplication app) {
		this.app = app;
	}

	@Override
	public Field createField(Item item, Object propertyId, Component uiContext) {
		Field f = null;
		if ("routeId".equals(propertyId)) {
			routeId.setRequired(true);
			routeId.setWidth(Constants.COMMON_FIELD_WIDTH);
			return routeId;
		} if ("dateCreated".equals(propertyId)) {
			dateCreated.setEnabled(false);
			dateCreated.setWidth(Constants.COMMON_FIELD_WIDTH);
			dateCreated.setDateFormat(Constants.DATE_FORMAT_STRING);
			dateCreated.setResolution(DateField.RESOLUTION_DAY);
			return dateCreated;
		} if ("routeStatus".equals(propertyId)) {
			try{
				routeStatus = (NativeSelect) super.createField(item, propertyId, uiContext);
			} catch (ClassCastException e) {
				JPAContainer<RouteStatus> container = JPAContainerFactory.make(RouteStatus.class, app.PERSISTENCE_UNIT);
				routeStatus.setContainerDataSource(container);
			}
			routeStatus.setNullSelectionAllowed(false);
			routeStatus.setItemCaptionMode(ComboBox.ITEM_CAPTION_MODE_ITEM);
			routeStatus.setRequired(true);
			routeStatus.setWidth(Constants.COMMON_FIELD_WIDTH);
			routeStatus.setRequiredError(routeStatus.getCaption()+" is required");
			return routeStatus;
		} if ("routeStart".equals(propertyId)) {
			try{
				routeStart = (NativeSelect) super.createField(item, propertyId, uiContext);
			} catch (ClassCastException e) {
				JPAContainer<Substation> container = JPAContainerFactory.make(Substation.class, app.PERSISTENCE_UNIT);
				routeStart.setContainerDataSource(container);
			}
			routeStart.setWidth(Constants.COMMON_FIELD_WIDTH);
			routeStart.setNullSelectionAllowed(false);
			routeStart.setItemCaptionMode(ComboBox.ITEM_CAPTION_MODE_ITEM);
			routeStart.setRequired(true);
			routeStart.setRequiredError(routeStart.getCaption()+" is required");
			return routeStart;
		} if ("routeEnd".equals(propertyId)) {
			try{
				routeEnd = (NativeSelect) super.createField(item, propertyId, uiContext);
			} catch (ClassCastException e) {
				JPAContainer<Substation> container = JPAContainerFactory.make(Substation.class, app.PERSISTENCE_UNIT);
				routeEnd.setContainerDataSource(container);
			}
			routeEnd.setWidth(Constants.COMMON_FIELD_WIDTH);
			routeEnd.setNullSelectionAllowed(false);
			routeEnd.setItemCaptionMode(ComboBox.ITEM_CAPTION_MODE_ITEM);
			routeEnd.setRequired(true);
			routeEnd.setRequiredError(routeEnd.getCaption()+" is required");
			return routeEnd;
		} else {
			// Use the super class to create a suitable field base on the
			// property type.
			f = super.createField(item, propertyId, uiContext);
		}
		return f;
	}
}
